<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="flex-1" :model="formState" :label-col="{ style: { width: '70px' } }">
        <a-row :gutter="24">
          <a-col :span="8">
            <a-form-item name="zjm">
              <a-input v-model:value="formState.zjm" placeholder="供应商编号/供应商名称/助记码" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div class="mb-3">
        <a-button type="primary" class="mr-2" @click="onAdd">新增</a-button>
        <BaseExport type="药库" name="供应商维护" code="00001006" :search-params="formState">导出</BaseExport>
      </div>
      <BaseTable :loading="loading" :data-source="tableData" :pagination="pagination" bordered fill column-code="00001006">
        <template #bodyCell="{ column, record, index }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onHandleEdit(record)"> 修改 </a-button>
            <a-divider type="vertical" />
            <ConfirmModal description="确定删除?" @confirm="onConfirm(record)">
              <a-button type="text" danger size="small"> 删除 </a-button>
            </ConfirmModal>
            <a-divider type="vertical" />
            <a-button type="link" size="small" @click="onHandleEdit(record, '2')"> 首营资质 </a-button>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" @success="getData" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'
import ConfirmModal from '@/components/ConfirmModal'
import Edit from './Edit'

const rowData = ref(null)
const editOpen = ref(false)
const formRef = ref(null)
const formState = ref({})
const onAdd = async () => {
  rowData.value = {}
  editOpen.value = true
}
const onConfirm = async (record) => {
  const { code, msg } = await post('/drugStorage/setting/doDeleteSupplierInfo', { gysbh: record.gysbh })
  if (code !== 0) return message.error(msg || '失败')
  message.success(msg)
  getData()
}

const onHandleEdit = (row, active = '1') => {
  rowData.value = row
  rowData.value.active = active
  editOpen.value = true
}

const { loading, tableData, pagination, getData } = useTable({
  url: '/drugStorage/comprehenive/getSupplierInfoList',
  immediate: true,
  params: formState.value
})
// const onSuceess = () => {
// editOpen.value = false
// queryList()
// }
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
